package com.example.mytips.openAssets;

import com.example.mytips.ImagePopupPlugin;
import com.example.mytips.assets.FlutterAssetsDialog;
import com.example.mytips.utils.DialogUtils;
import com.example.mytips.utils.ShowInFinder;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.event.EditorMouseEvent;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import org.apache.http.util.TextUtils;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;

public class OpenAssetsFile extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        ///工程
        Project project = e.getProject();
        if (project == null) return;

        ///上下文
        DataContext context = e.getDataContext();

        Editor editor = CommonDataKeys.EDITOR.getData(context);
        String selectedText = editor.getSelectionModel().getSelectedText();

        ///如果选中的是assets,或者是以assets打头的完整图片路径
        if (selectedText != null && selectedText.startsWith(ImagePopupPlugin.startSymbol)) {

            String basePath = project.getBasePath();

            String imgPath = "";
            if (selectedText.length() == ImagePopupPlugin.startSymbol.length()) {
                ///选中的是assets
                String lineStr = lineStr(editor);
                String lineTrim = lineStr.trim();

                String replaced = lineTrim.replace(ImagePopupPlugin.startSymbol, "");
                if (lineTrim.length() - replaced.length() == ImagePopupPlugin.startSymbol.length()) {
                    String between1 = findSubstringBetween(lineTrim, ImagePopupPlugin.startSymbol, "'");
                    String between2 = findSubstringBetween(lineTrim, ImagePopupPlugin.startSymbol, "\"");
                    System.out.println("between1: " + between1 + ",between2: " + between2);

                    imgPath = between1;
                    if (TextUtils.isEmpty(between1)) {
                        imgPath = between2;
                    }

                    imgPath = basePath + "/" + ImagePopupPlugin.startSymbol + imgPath;

                } else {
                    System.out.println("一行包含多个，得用全路径先这中才行");
                }

            } else {
                ///以assets打头的完整图片路径
                imgPath = basePath + "/" + selectedText;
            }

            ShowInFinder.showInFiner(imgPath, e);

        }
    }

    /// 查找两者之间的字符串
    private static String findSubstringBetween(String mainString, String subString1, String subString2) {
        int index1 = mainString.toLowerCase().indexOf(subString1.toLowerCase());
        int index2 = mainString.toLowerCase().indexOf(subString2.toLowerCase(), index1 + subString1.length());

        if (index1 != -1 && index2 != -1) {
            return mainString.substring(index1 + subString1.length(), index2);
        } else {
            return null;
        }
    }

    private String lineStr(Editor editor) {
        Document document = editor.getDocument();
        CaretModel caretModel = editor.getCaretModel();
        int caretOffset = caretModel.getOffset();
        int lineNumber = document.getLineNumber(caretOffset);
        int lineStartOffset = document.getLineStartOffset(lineNumber);
        int lineEndOffset = document.getLineEndOffset(lineNumber);
        return document.getText(new TextRange(lineStartOffset, lineEndOffset));
    }
}
